// pages/device/workflowSetting/workflowSetting.js
import lwx from '../../../utils/lwx.js'
import {
  _globle
} from '../../../utils/globle.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    switch1Checked: true,
    switch2Checked: false,
    switch3Checked: false,
    // defaultData: {
    //   name: '默认流程',
    //   level: '',
    //   person: []
    // },
    orderArrayData: {},
    specialData: {},
    workflow: {
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.loadData();
  },
  loadData: function() {
    var that = this;
    lwx.request({
      url: "seal.sealworkflow",
      data: {
        // companyId: _globle.user.companyId,
        // partyId: _globle.user.partyId
        companyId: 2,
        partyId: 2,
      }
    }).then(res => {
      console.log(res)
      if (res.data.code == '0') {
        that.setData({
          devices: res.data.devices
        })
      }
    }).catch(err => {
      console.log('err' + err);
    })
  },
  switchChange: function(e) {
    // var switch1Checked = this.data.switch1Checked;
    var that = this;
    var workflow = that.data.workflow
    var switch2Checked = that.data.switch2Checked;
    var switch3Checked = that.data.switch3Checked;
    switch (e.currentTarget.dataset.name) {
      // case '1':
      //   switch1Checked = e.detail.value;
      //   switch2Checked = false;
      //   this.setData({
      //     switch1Checked: switch1Checked,
      //     switch2Checked: switch2Checked,
      //   })
      //   return;
      case '2':
        if (Object.keys(that.data.orderArrayData).length === 0) {
          wx.showModal({
            content: '请先选择流程',
            showCancel: false,
            success: function(res) {
              that.setData({
                switch2Checked: false,
              })
            },
          })
          return;
        }
        // switch1Checked = false;
        switch2Checked = e.detail.value;
        workflow.workflowType = "QUEUE";
        workflow.workflowId = that.data.orderArrayData.workflowId;
        this.setData({
          // switch1Checked: switch1Checked,
          switch2Checked: switch2Checked,
          workflow: workflow
        })
        return;
      case '3':
        if (Object.keys(this.data.specialData).length === 0) {
          wx.showModal({
            content: '请先选择流程',
            showCancel: false,
            success: function(res) {
              that.setData({
                switch3Checked: false,
              })
            },
          })
          return;
        }
        workflow.specialworkflowId = this.data.specialData.workflowId;
        switch3Checked = e.detail.value;
        this.setData({
          switch3Checked: switch3Checked,
          workflow: workflow
        })
        return;
    }
  },
  //更换顺序流程
  changeWorkflow: function() {
    wx.navigateTo({
      url: '../orderWorkflow/orderWorkflow?select=1',
    })
  },
  //更换特批流程
  changeWorkflow2: function() {
    wx.navigateTo({
      url: '../specialWorkflow/specialWorkflow?select=0',
    })
  },

  keepit: function() {
    console.log(this.data.workflow)
    if (this.data.switch2Checked || this.data.switch3Checked) {
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2];
      prevPage.setData({
        workflow: this.data.workflow
      })
      wx.navigateBack({
        delta: 1,
      })
    } else {
      wx.showModal({
        content: '请先选择流程',
        showCancel: false,
        success: function(res) {},
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function(options) {

    var workflow = this.data.workflow
    if (this.data.switch2Checked) {
      workflow.workflowType = "QUEUE";
      workflow.workflowId = this.data.orderArrayData.workflowId;
    }
    if (this.data.switch3Checked) {
      workflow.specialworkflowId = this.data.specialData.workflowId;
    }
    this.setData({
      workflow: workflow
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})